package com.example.charactivity.ChartDaily

import android.app.usage.UsageStats
import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.example.charactivity.GernerChartModel
import com.example.recyclerview.UsingData
import com.github.aachartmodel.aainfographics.aachartcreator.AAChartType
import com.github.aachartmodel.aainfographics.aachartcreator.AASeriesElement
import java.util.*

class DayChartViewModel : ViewModel() {

    var str=""
    var millisTime= MutableLiveData<Long>()
    lateinit var nowTime:Calendar
    companion object daily{
        var arrayData= arrayOfNulls<Long>(6)
        var arrayPackName= arrayOfNulls<String>(6)
        var appinfo= arrayListOf<UsingData>()
        fun configureChart() {
            /*仅仅更新了图表的series数组数据,不改动图表的其他内容*/
            val n1: Long = arrayData[0]!!
            val n2:Long=arrayData[1]!!
            val n3:Long= arrayData[2]!!
            val n4:Long = arrayData[3]!!
            val n5:Long = arrayData[4]!!
            val n6:Long =arrayData[5]!!
            GernerChartModel.aaChartModel.series(
                    arrayOf(
                            AASeriesElement()
                                    .showInLegend(false)
                                    .name("today")
                                    .color("#f48fb1")
                                    .type(AAChartType.Pie)
                                    .data(arrayOf(n1,n2,n3,n4,n5,n6))
                    )

            )
                    .dataLabelsEnabled(false)
                    .subtitle("the use time of the day")
        }

    }

    fun sortData(usageStatsList: MutableList<UsageStats>) :Long{
        // 开启一个后台协程
        str=""
        appinfo.clear()
        // 每次查询先清空
        usageStatsList.sortBy {
            it.totalTimeInForeground
        } // 排序
         usageStatsList.reverse()
        val top12:List<UsageStats>
        var MSize=usageStatsList.size
        if(usageStatsList.size>=12)  // 获取前12个！
        {
            top12=usageStatsList.subList(0,12)
            MSize=12
        }
        else
            top12=usageStatsList
        for(its in top12){

            // 获取前12个最常用的
           val h=its.totalTimeInForeground/60/60/1000
            // 得到小时的数目
            var min:Int
            if(h>0){
                min= ((its.totalTimeInForeground/60/1000)%h).toInt()
            }
            else
                min= (its.totalTimeInForeground/60/1000).toInt()

            val str2=its.packageName.replace("com.","")
            // 删除某个字符串
            val temp=UsingData(str2,"${h}h${min}min")
            appinfo.add(temp)
        }

        val moreUsed:List<UsageStats>
        var maxSize=usageStatsList.size
        Log.d("bpt","total:${maxSize}")
        if(maxSize>6)  // 获取前6个！
        {
            moreUsed=usageStatsList.subList(0,6)
            maxSize=6
        }
        else
        {
            moreUsed=usageStatsList

        }
// 完成排序
        var i=0
        Log.i("bpt","size:${moreUsed.size}")
        for (usageStats in moreUsed) {
            DayChartViewModel.arrayData[i]= usageStats.totalTimeInForeground/60/1000
            // 单位分钟
          //  Log.d("search",arrayData[i].toString())
            arrayPackName[i]=usageStats.packageName
            str=str+"package:${arrayPackName[i]}:time:${arrayData[i]}\n"
          //  Log.d("search", "package:${arrayPackName[i]}:time:${arrayData[i]}")

            //  usageStatics. printState(usageStats)

            i++   // 气死你了，忘记循环了！！！！！！！！！
        }
        // 得到数据
        if(i>0)
        return  arrayData[i-1]!!
        else
            return 0
        // 通过此处来回应 查询完毕
    }

}